﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Guids;
using Volo.Abp.Linq;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
using XDQ.VAT.Courses;

namespace XDQ.VAT.DataSeeder
{
    public class VATEduDataSeedContributor : IDataSeedContributor, ITransientDependency
    {
        public VATEduDataSeedContributor(ILogger<VATEduDataSeedContributor> logger, ICurrentTenant currentTenant, IAsyncQueryableExecuter queryableExecuter, IGuidGenerator guidGenerator, IUnitOfWorkManager unitOfWorkManager, IAppCourseRepository courses)
        {
            Logger = logger;
            CurrentTenant = currentTenant;
            QueryableExecuter = queryableExecuter;
            GuidGenerator = guidGenerator;
            UnitOfWorkManager = unitOfWorkManager;
            Courses = courses;
        }

        public ILogger<VATEduDataSeedContributor> Logger { get; }
        public ICurrentTenant CurrentTenant { get; }
        public IAsyncQueryableExecuter QueryableExecuter { get; }
        public IGuidGenerator GuidGenerator { get; }
        public IUnitOfWorkManager UnitOfWorkManager { get; }
        public IAppCourseRepository Courses { get; }

        [UnitOfWork]
        public async Task SeedAsync(DataSeedContext context)
        {
            using var tenant = CurrentTenant.Change(context.TenantId);
            if (!await QueryableExecuter.AnyAsync(Courses, x => true))
            {
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "语文",1));
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "数学",2));
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "英语",3));
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "体育",4));
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "音乐",5));
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "美术",6));
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "物理",7));
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "化学",8));
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "生物",9));
                await Courses.InsertAsync(new AppCourse(GuidGenerator.Create(), "地理",10));
                await UnitOfWorkManager.Current.SaveChangesAsync();
            }
        }
    }
}
